import { Schema } from "mongoose";
import type Typings from "../typings";

import { createDb } from "../mongoose/mongoose";
const db = createDb("service", "button");

// 定义 Schema 模型
const ButtonSchema = new Schema<Typings.Button.props>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  parentKey: {
    type: String,
    required: true,
    default: '',
  },

  title: {
    type: String,
    required: true,
    default: '',
  },

  key: {
    type: String,
    required: true,
    unique: true,
    default: '',
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
ButtonSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

ButtonSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret: any) => {
    delete ret._id;
    delete ret.__v;
  },
});

const ButtonModel = db.model<Typings.Button.props>("Button", ButtonSchema, "buttons");

export default ButtonModel;
